//
//  QLLoginModel.m
//  天使配送
//
//  Created by qlwl on 16/6/13.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLLoginModel.h"
#import "NSString+Judge.h"
#import "NSString+Extension.h"

@implementation QLLoginModel
+(NSDictionary*)mj_replacedKeyFromPropertyName
{
    return @{@"userId":@"id"};
}


- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.userId forKey:@"UserId"];
    [encoder encodeObject:self.mobilephone forKey:@"MobilePhone"];
    [encoder encodeObject:self.levelponit forKey:@"LevelPonit"];
    [encoder encodeObject:self.age forKey:@"Age"];
    [encoder encodeObject:self.sex forKey:@"Sex"];
    [encoder encodeObject:[NSNumber numberWithInt:self.authenticationtype] forKey:@"AuthenticationType"];
    [encoder encodeObject:self.isdelted forKey:@"IsDelted"];
    [encoder encodeObject:self.headpicurl forKey:@"HeadPicUrl"];
    [encoder encodeObject:self.birthday forKey:@"BirthDay"];
    [encoder encodeObject:[NSNumber numberWithInt:self.isauthentication] forKey:@"IsAuthentication"];
    [encoder encodeObject:self.idbackurl forKey:@"IdBackUrl"];
    [encoder encodeObject:self.name forKey:@"Name"];
    [encoder encodeObject:self.idfronturl forKey:@"IdFrontUrl"];
    [encoder encodeObject:self.password forKey:@"PassWord"];
    [encoder encodeObject:self.usertype forKey:@"UserType"];
    [encoder encodeObject:self.createtime forKey:@"CreateTime"];
    [encoder encodeObject:self.updatetime forKey:@"UpdateTime"];
    [encoder encodeObject:self.yx_accid forKey:@"YX_Accid"];
    [encoder encodeObject:self.yx_token forKey:@"YX_Token"];
}

// 解档的时候调用
- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        self.userId = [decoder decodeObjectForKey:@"UserId"];
        self.mobilephone = [decoder decodeObjectForKey:@"MobilePhone"];
        self.levelponit = [decoder decodeObjectForKey:@"LevelPonit"];
        self.age = [decoder decodeObjectForKey:@"Age"];
        self.sex = [decoder decodeObjectForKey:@"Sex"];
        self.authenticationtype = [[decoder decodeObjectForKey:@"AuthenticationType"] intValue];
        self.isdelted = [decoder decodeObjectForKey:@"IsDelted"];
        self.headpicurl = [decoder decodeObjectForKey:@"HeadPicUrl"];
        self.birthday = [decoder decodeObjectForKey:@"BirthDay"];
        self.isauthentication = [[decoder decodeObjectForKey:@"IsAuthentication"] intValue];
        self.idbackurl = [decoder decodeObjectForKey:@"IdBackUrl"];
        self.name = [decoder decodeObjectForKey:@"Name"];
        self.idfronturl = [decoder decodeObjectForKey:@"IdFrontUrl"];
        self.password = [decoder decodeObjectForKey:@"PassWord"];
        self.usertype = [decoder decodeObjectForKey:@"UserType"];
        self.createtime = [decoder decodeObjectForKey:@"CreateTime"];
        self.updatetime = [decoder decodeObjectForKey:@"UpdateTime"];
        self.yx_accid = [decoder decodeObjectForKey:@"YX_Accid"];
        self.yx_token = [decoder decodeObjectForKey:@"YX_Token"];
    }
    return self;
}

// 存储当前授权模型
- (BOOL)save{
    //保存登录模式
    // 1.获取存储路径
    NSString *filePath = [@"QLLoginModel.data" appendDocumentDir];
    // 2存储数据
    BOOL flag = [NSKeyedArchiver archiveRootObject:self toFile:filePath];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:LoginModelChangedNotification object:self];
    return flag;
}
// 读取当前授权模型
+ (instancetype)accountFromSandbox{
    // 1.获取存储路径
    NSString *filePath = [@"QLLoginModel.data" appendDocumentDir];
    // 2.解归档授权信息
    QLLoginModel *account = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    return account;
}
+(BOOL)endLogin
{
    NSString *filePath = [@"QLLoginModel.data" appendDocumentDir];
    NSError *error;
    return [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}

+ (BOOL)isLogin
{
    // 1.获取存储路径
    NSString *filePath = [@"QLLoginModel.data" appendDocumentDir];
    // 2.解归档授权信息
    QLLoginModel *account = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if (account) {
        return YES;
    }
    return NO;
}
+(void)inquiryUserInfoById:(NSString *)userId andReceivedInfo:(void(^)(NSDictionary * result))received
{
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"userid"] = userId;
    [QLNetWorking postJSONWithUrl:QLInquiryUserInfoURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        if (received) {
            received(result);
        }
    } fail:^(NSError *error) {
        if (received) {
            received(nil);
        }
    }];
}

@end
